/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package controle;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.Date;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import modele.*;

/**
 *
 * @author sio
 */
public class Controle extends HttpServlet {

    private Pays pays;

    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        pays = new Pays(); // instancie les objets utiles
    }

    public void destroy() {
        Connect.close();
    }

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        String uc = request.getParameter("uc");
        String page;
        switch (uc) {
            case "dep":
                if (request.getParameter("depa") == null) {
                    page = "Dep.jsp";

                    request.setAttribute("liste", pays.getLesDeps());
                    request.getRequestDispatcher(page).forward(request, response);
                } else {
                    page = "Dep2.jsp";
                    String code = request.getParameter("depa");
                    request.setAttribute("liste2", pays.getLeDep(code).getLesmeds());
                    request.getRequestDispatcher(page).forward(request, response);
                }
                break;
            case "nom":
                if (request.getParameter("nom") == null) {
                    page = "Nom.jsp";
                    request.getRequestDispatcher(page).forward(request, response);
                } else {
                    page="Nom2.jsp";
                    String text=request.getParameter("nom");
                    request.setAttribute("liste3", pays.getByNom(text));
                    request.getRequestDispatcher(page).forward(request, response);
                }
                break;
            case "spe" :
                if (request.getParameter("spe") == null) {
                    page = "Spe.jsp";
                    
                    request.setAttribute("liste", pays.getLesSpes());
                    request.getRequestDispatcher(page).forward(request, response);
                } else {
                    page="Spe2.jsp";
                    
                    String nom = request.getParameter("spe");
                    request.setAttribute("liste4", pays.getLeSpe(nom).getLesmeds());
                    request.getRequestDispatcher(page).forward(request, response);
                }
                
                break;

        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
